package com.example.blog.controller;

import com.example.blog.domain.Users;
import com.example.blog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.Data;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


@RestController
public class BaseController {
    public HttpServletRequest getHttpInfo(){
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
    public Users getLogSign(){
         HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
         Claims claims = JwtUtils.checkToken(request.getHeader("token"));
         Users users = new Users();
         System.err.println(claims);
         users.setUid( Long.parseLong(String.valueOf(claims.get("uid"))));
         users.setUsername(String.valueOf(claims.get("username")));
         return users;
    }
}
